#coding:UTF-8


'''
Created on 2015年4月1日

@author: Jeffrey Chu
'''

import re

from django.http.response import HttpResponseRedirect
from django.db.models import Q

from lib.utils import do_logging
from account.models import Message as MessageDb, User as UserDb
from forum.models import  Sharing as SharingModel



class HandleMessage(object):
    def process_response(self, request, response):
        if request.method=="POST":
            return response
        
        if re.search(r'.+/messages/', request.path):
            try:
                userId=request.session.get('userId','')
                user=UserDb.objects.get(id=userId,status=True)
                msgList=MessageDb.objects.filter(Q(originUser=user)|Q(toPer=user)).exclude(fromPer=user).order_by('-createTime')
                for msg in msgList:
                    msg.status=False
                    msg.save()
                return response
            #未登录
            except Exception as e:
                do_logging("error",e ,"User has not login!")
                return HttpResponseRedirect('/account/login_page/')
        
        
        S=re.search(r'/forum/(?P<article_id>\d+)/$', request.path)
        if S:
            try:
                article_id=S.groupdict().get('article_id',0)
                article=SharingModel.objects.get(id=article_id)
                article.all_scan_count+=1
                article.week_scan_count+=1
                article.save()
                return response
            except Exception as e:
                do_logging("error", e)
        else:
            return response
        